// list.js
const AV = require('../../../utils/av-weapp.js')

function initSubMenuDisplay() {
  return ['hidden','hidden','hidden'];
}

var initSubMenuHighLight = [
  ['','','','',''],
  ['',''],
  ['','','']
];

var pageIndex = 0;
var that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    subMenuDisplay:initSubMenuDisplay(),
    subMenuHighLight:initSubMenuHighLight,
    goods:[],
    loadingTip:'上拉加载更多'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    var categoryId = options.categoryId;
    //生成category对象
    var category = AV.Object.createWidthoutData('Category',categoryId);
    this.category = category;
    this.getGoods(category,0);
  },

  /**
   * 用页数获取分类category
   */
  getGoods:function(category,pageIndex) {
    // 一页的数量
    var pageSize = 7;
    // 查询Good
    var query = new AV.Query('Goods');
    // 查询顶级分类，设计查询条件parent为null
    query.equalTo('category',category);
    // 分页查询
    query.limit(pageSize); //最多返回10条结果
    query.skip(pageIndex * pageSize) //跳过20条结果
    query.find().then(function(goods) {
      wx.stopPullDownRefresh();
      var originGoods = that.data.goods;
      var newGoods = originGoods.length > 0 ? originGoods.concat(goods) : goods
      that.setData({
        goods:newGoods
      });
      if(goods.length < pageSize) {
        that.setData({
          loadingTip:'没有更多内容'
        });
      }
    }).catch(function(error){
    });
  },

/**
 * click goods
 */
  tapGoods:function(e) {
    var objectId = e.currentTarget.dataset.objectId;
    wx.navigateTo({
      url: '../detail/detail?objectId=' + objectId,
    });
  },

/**
 * click 主菜单
 */
  tapMainMenu:function(e) {
    //获取当前显示的一级菜单标识
    var index = parseInt(e.currentTarget.dataset.index);
    //生产数据，全部为hidden的，只对当前的进行显示
    var newSubMenuDisplay = initSubMenuDisplay();

    if(this.data.subMenuDisplay[index] == 'hidden') {
      newSubMenuDisplay[index] = 'show';
    } else {
      newSubMenuDisplay[index] = 'hidden';
    }

    this.setData({
      subMenuDisplay:newSubMenuDisplay
    })
  },

/**
 * click
 *  子菜单
 */
  tapSubMenu:function(e) {
    this.subData({
      subMenuDisplay:initSubMenuDisplay()
    });

    var indexArray = e.currentTarget.dataset.index.split('-');
    for(var i = 0;i<initSubMenuHighLight.length;i++) {
      if(indexArray[0] == i) {
        for(var j = 0;j<initSubMenuHighLight[i].length;j++) {
          initSubMenuHighLight[i][j] = '';
        }
      }
    }

    initSubMenuHighLight[indexArray[0]][indexArray[1]] = 'highlight';
    this.setData({
      subMenuHighLight:initSubMenuHighLight
    })
  },

  onReachBottom:function() {
    setTimeout(function() {
      that.getGoods(that.category,++pageIndex);
    },300);
  },

  onPullDownRefresh:function() {
    this.getGoods(this.category,0);
  },

  addCard:function(e) {
    var objectId = e.currentTarget.dataset.objectId;
    var goods = AV.Object.createWidthoutData('Goods',objectId);
    this.insertCart(goods);
  },

  insertCart:function(goods) {
    var that = this;
    var user = AV.User.crrent();

    var query = new AV.Query('Cart');
    query.equalTo('user',user);
    query.equalTo('goods',goods);
    query.count().then(function (count) {
      if(count <= 0) {
        var cart = AV.Object('Cart');
        cart.set('user',user);
        cart.set('auantity',1);
        cart.set('goods',goods);
        cart.save().then(function(cart) {
          that.showCartToast();
        },function(error) {
          console.log(error);
        }
        );
      } else {
        query.first().then(function (cart) {
          cart.increment('quantity',1);
          that.showCartTaost();
        },function(error) {
          console.log(error);
        }
        );
      }
    },function(error) {

    });
  },

  /**
   * show toast
   */
  showCartToast:function() {
    wx.showToast({
      title: '已加入购物车',
      icon:'success',
      duration:1000
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})